<?php
class Controller_Purchase_Product extends Controller_Abstract
{
	function actionMultiUpdateStatus()
	{
		$this->_view['title'] = '批量修改状态';
		$warehouse = Warehouse::getActiveWarehouse();
		$this->_view['warehouse'] = $warehouse;
	}
	
	function actionCheckNumberExists()
	{
		$this->_view['title'] = '检查产品编号是否有效';
		if (isset($_REQUEST['product_number']) && isset($_REQUEST['warehouse_id']))
		{
			$product_basic_number = Q::normalize($_REQUEST['product_number'], "\n");
			$result = Product_Warehouse::checkExists($product_basic_number, $_REQUEST['warehouse_id']);
			if ($result['ack'] == SUCCESS)
			{
				echo 1;exit;
			}
			else
			{
				echo json_encode($result['not_exists']);exit;
			}
		}
		else
		{
			echo -1;exit;
		}
	}
	
	function actionMultiUpdateStatusSave()
	{
		$this->_view['title'] = '批量修改状态提交';
		if ($this->_context->isPOST())
		{
			$product_basic_number = isset($_POST['multi_product_basic_number']) ? $_POST['multi_product_basic_number'] : '';
			$purchase_status = isset($_POST['purchase_status']) ? $_POST['purchase_status'] : '';
			$warehouse_id = isset($_POST['warehouse_id']) ? $_POST['warehouse_id'] : '';
			if ($warehouse_id && $product_basic_number && $purchase_status)
			{
				$product_basic_number = Q::normalize($product_basic_number, "\n");
				$result = Product_Warehouse::checkExists($product_basic_number, $warehouse_id);
				if ($result['ack'] == SUCCESS)
				{
					Product_Warehouse::multiUpdateProductStatus($product_basic_number, $warehouse_id, 'purchase', $purchase_status);
					return $this->_redirectMessage('修改成功', '正在返回', $_SERVER['HTTP_REFERER'], $result['ack']);
				}
				else
				{
					$product_basic_number = array_diff($product_basic_number, $result['not_exists']);
					if (!empty($product_basic_number))
					{
						Product_Warehouse::multiUpdateProductStatus($product_basic_number, $warehouse_id, 'purchase', $purchase_status);						
					}
					return $this->_redirectMessage('修改失败', '产品编号为' . implode(',', $result['not_exists']) . '不存在', $_SERVER['HTTP_REFERER'], FAILURE);
				}
			}
			else
			{
				return $this->_redirectMessage('修改失败', '仓库或产品编号或状态选择不能为空', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}

	function actionMultiuCreateProductWarehouse()
	{
		$this->_view['title'] = '批量库存资料';
		$warehouse = Warehouse::find()->asArray()->getAll();
		$this->_view['warehouse'] = $warehouse;
	}
	
	function actionMultiuCreateProductWarehouseSave()
	{
		$this->_view['title'] = '批量库存资料（保存）';
		$product_basic_number = explode("\n", $_POST['product_number']);
		foreach ($product_basic_number as &$val)
		{
			$val = trim($val);
		}
		unset($val);
		$create_result = Product_Warehouse::create($product_basic_number, $_POST['select_warehouse']);
		if ($create_result['ack'] == SUCCESS)
		{
			return $this->_redirectMessage('保存成功', '正在返回..',  $_SERVER['HTTP_REFERER'], $create_result['ack']);
		}
		if ($create_result['ack'] == PARTIAL_FAILURE)
		{
			return $this->_redirectMessage('保存失败', $create_result['message'], $_SERVER['HTTP_REFERER'], FAILURE);
		}
		else
		{
			return $this->_redirect($_SERVER['HTTP_REFERER']);
		}
	}
}